package com.yaosheng.thread.thread;

import java.util.concurrent.TimeUnit;

/**
 * Created by yaosheng on 2021/7/2.
 * ThreadLocal:当前线程独享该内存，其他线程无法访问
 */
public class ThreadLocal1 {

    static ThreadLocal<Person> threadLocal = new ThreadLocal<> ();

    public static void main(String[] args) {

        new Thread ( () -> {
            try{
                TimeUnit.SECONDS.sleep (2);
            } catch (InterruptedException e) {
                e.printStackTrace ();
            }
            System.out.println (threadLocal.get());
        }).start ();

        new Thread ( () -> {
            try{
                TimeUnit.SECONDS.sleep (1);
            } catch (InterruptedException e) {
                e.printStackTrace ();
            }
            threadLocal.set (new Person ("姚圣"));
        }).start ();
    }

    static class Person {

        private String name;

        public Person(String name){
            name = this.name;
        }
    }
}
